//app.js
App({
  onLaunch: function () {
    var that = this;
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

  },

  request: function () {

  },

  getTimestamp: function () {
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    return timestamp;
  },

  getRandom: function () {
    var random = Math.round(Math.random() * 2);
    return random;
  },

  /**
   *处理游客访问权限的问题
   */
  checkAccessAuthority: function (opNum) {
    var that = this;
    var userType = that.globalData.userType;
    var list = that.globalData.accessAuthorityList;

    for (var i = 0; i < list.length; i++) {
      if (list[i].opNum == opNum) {
        if (list[i].userType != userType) {
          return false;
        }
      }
    }
    return true;
  },
  /**
   * 处理tabBar
   */
  editTabBar: function () {
    var _curPageArr = getCurrentPages();
    var _curPage = _curPageArr[_curPageArr.length - 1];
    var _pagePath = _curPage.__route__;
    if (_pagePath.indexOf('/') != 0) {
      _pagePath = '/' + _pagePath;
    }
    var tabBar = null;
    if ((this.globalData.userType == 1 && this.globalData.companyStatus == null) || this.globalData.userType == 2) {
      tabBar = this.globalData.tabBar;
    } else {
      tabBar = this.globalData.tabBar2;
    }


    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == _pagePath) {
        tabBar.list[i].active = true;//根据页面地址设置当前页面状态    
      }
    }
    _curPage.setData({
      tabBar: tabBar
    });
  },
  /**
   * 更新用户信息
   */
  updateUserInfo: function () {
    var that = this;
    if (that.globalData.userType == 2) {
      wx.request({
        url: that.globalData.server + '/user/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + that.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': that.getTimestamp(),
          'x-wxapp-nonce': that.getRandom()
        },
        success: function (res) {
          that.globalData.userInfo = res.data.data;
          that.globalData.userInfo.expert_avatar = that.globalData.server + res.data.data.expert_avatar;
        }
      })
    } else if (that.globalData.userType == 3) {
      wx.request({
        url: that.globalData.server + '/user/',
        method: 'GET',
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded', // 默认值
          'Authorization': 'Bearer ' + that.globalData.token,
          'x-wxapp-signature': null,
          'x-wxapp-timestamp': that.getTimestamp(),
          'x-wxapp-nonce': that.getRandom()
        },
        success: function (res) {
          that.globalData.userInfo = res.data.data;
          that.globalData.userInfo.company_logo = that.globalData.server + res.data.data.company_logo;
        }
      })
    }
  },
  /**
   * globalData
   */
  globalData: {
    server: "https://doclinzh.com",
    //server: "https://qydp.net.cn",


    temp: null,
    token: null,

    userInfo: null,
    userType: null,

    companyStatus: null,
    reason: null,

    thisYear: null,
    currentYearRewardInfo: {
      reward_id: null,
      reward_amount: null,
      hasLaunched: false,
      addition: false,
    },

    /**
     *  导航栏样式1&2
     */
    tabBar: {
      "color": "#7e7e7e",
      "selectedColor": "#ff8817",
      "backgroundColor": "#fff",
      "borderStyle": "#e7e7e7",
      "list": [
        {
          "pagePath": "/pages/index/index",
          "text": "点评首页",
          "iconPath": "/img/tabBar/tabBarIcon0.png",
          "selectedIconPath": "/img/tabBar/tabBarIcon1.png",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/logs/logs",
          "text": "我的发布",
          "iconPath": "/img/tabBar/tabBarIcon3.png",
          "selectedIconPath": "/img/tabBar/tabBarIcon4.png",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/userCenter/userCenter",
          "text": "个人中心",
          "iconPath": "/img/tabBar/tabBarIcon5.png",
          "selectedIconPath": "/img/tabBar/tabBarIcon6.png",
          "clas": "menu-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    tabBar2: {
      "color": "#7e7e7e",
      "selectedColor": "#ff8817",
      "backgroundColor": "#fff",
      "borderStyle": "#e7e7e7",
      "list": [
        {
          "pagePath": "/pages/companyIndex/companyIndex",
          "text": "点评首页",
          "iconPath": "/img/tabBar/tabBarIcon0.png",
          "selectedIconPath": "/img/tabBar/tabBarIcon1.png",
          "clas": "menu-item2",
          active: false
        },
        {
          "pagePath": "/pages/companyCenter/companyCenter",
          "text": "企业中心",
          "iconPath": "/img/tabBar/tabBarIcon5.png",
          "selectedIconPath": "/img/tabBar/tabBarIcon6.png",
          "clas": "menu-item2",
          active: false
        }
      ],
      "position": "bottom"
    },

    /**
     * 访问权限列表
     */
    accessAuthorityList: [
      {
        // 浏览 我的发布page
        "opNum": 1,
        "userType": 2,
      }, {
        // 浏览 发起预测点评子page
        "opNum": 2,
        "userType": 2,
      },
    ],
  },
})